/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-11
 * Time: 20:24
 */

import java.util.*;


class Solution2_1 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param ss string字符串
     * @return bool布尔型
     */
    public boolean isValid (String ss) {
        // write code here
        Stack<Character> stack = new Stack<>();
        char[] s = ss.toCharArray();

        for(char ch : s){
            if(ch == '('){
                stack.push(')');
            }else if(ch == '{'){
                stack.push('}');
            }else if(ch == '['){
                stack.push(']');
            }else if(stack.isEmpty() || ch != stack.pop()){
                return false;
            }else{
                // 这里的条件已经是出栈了，所以不用再写pop方法了
            }
        }
        return stack.isEmpty();
    }
}
public class Test2_1 {

}
